﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MyMoviesBrowserLibrary;

namespace TVManager
{
    /// <summary>
    /// Interaction logic for ChooseSeriesWindow.xaml
    /// </summary>
    /// 
    public delegate void SeriesSelectedEvent(object sender, int seriesId);
    public partial class ChooseSeriesWindow : Window
    {
        public event SeriesSelectedEvent SeriesSelected;
        public List<Series> Series { get; set; }
        public ChooseSeriesWindow()
        {
            InitializeComponent();
            selectButton.Click += new RoutedEventHandler(selectButton_Click);
            cancelButton.Click += new RoutedEventHandler(cancelButton_Click);
            listingsListBox.MouseDoubleClick += new MouseButtonEventHandler(listingsListBox_MouseDoubleClick);

            Loaded += new RoutedEventHandler(ChooseSeriesWindow_Loaded);
        }

        void listingsListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SeriesSelected2();
        }

        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (SeriesSelected != null)
                SeriesSelected(this,0);
        }

        void selectButton_Click(object sender, RoutedEventArgs e)
        {
            SeriesSelected2();
        }

        void SeriesSelected2()
        {
            Series series = listingsListBox.SelectedItem as Series;
            if (series == null)
                return;

            if (SeriesSelected != null)
                SeriesSelected(this, series.SeriesId);
        }

        void ChooseSeriesWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (Series.Count == 0)
                Close();
            else if (Series.Count == 1)
            {
                if (SeriesSelected != null)
                    SeriesSelected(this,Series.First().SeriesId);
            }
            else
            {
                listingsListBox.ItemsSource = Series;
            }
        }
    }
}
